package com.yohann.eduservice.controller;


import com.yohann.commonutils.result.R;
import com.yohann.eduservice.entity.EduVideo;
import com.yohann.eduservice.client.VodClient;
import com.yohann.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/edu-service/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节
     * @param video
     * @return
     */
    @PostMapping("add")
    public R addVideo(@RequestBody EduVideo video){
        videoService.save(video);

        return R.ok();
    }

    /**
     * 删除小节
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteVideo(@PathVariable String id){
        //获取video信息
        EduVideo video = videoService.getById(id);

        if(!StringUtils.isEmpty(video.getVideoSourceId())){
            //删除视频
            vodClient.deleteVideo(video.getVideoSourceId());
        }

        //删除数据
        videoService.removeById(id);

        return R.ok();
    }

    /**
     * 修改小节
     * @param video
     * @return
     */
    @PostMapping("update")
    public R updateVideo(@RequestBody EduVideo video){
        videoService.updateById(video);

        return R.ok();
    }

    /**
     * 通过id获取小节信息
     * @param id
     * @return
     */
    @GetMapping("getVideo/{id}")
    public R getVideo(@PathVariable String id){
        return R.ok().data("video",videoService.getById(id));
    }
}

